const fs = require("fs");
const shell = require("shelljs");
require("colors");

// * 获取当前版本信息
const packageJson = JSON.parse(fs.readFileSync("../package.json", "utf8"));
const version = packageJson.version;
// * 打包docker镜像
console.log("版本为".brightBlue, packageJson.version);

const newDirectory = "../";
try {
  process.chdir(newDirectory);
  console.log("当前工作目录已更改为:", process.cwd());
} catch (err) {
  console.error("更改工作目录时出错:", err);
}

if (
  shell.exec(
    `docker build . -t sh-platform-registry-vpc.cn-shanghai.cr.aliyuncs.com/shanghang/d3-app-cae-front:${version}`
  ).code !== 0
) {
  shell.echo("镜像打包失败");
  shell.exit(1);
}

if (
  shell.exec(
    `docker push sh-platform-registry-vpc.cn-shanghai.cr.aliyuncs.com/shanghang/d3-app-cae-front:${version}`
  ).code !== 0
) {
  shell.echo("镜像推送失败");
  shell.exit(1);
}

console.log("✅  镜像打包推送成功".brightGreen);
